//
//  EditShippingAddressCell.m
//  bestkeep
//
//  Created by bj on 2016/11/7.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "BK_my_selectedContactPeopleCell.h"
#import "Masonry.h"
#import "BKMyCommon.h"
#import "BK_my_ShippingAddress.h"
#import "BK_my_ContactPeople.h"
#import "BK_Adress_Tool.h"

@interface BK_my_selectedContactPeopleCell ()<UITextFieldDelegate, UIAlertViewDelegate>

@end

@implementation BK_my_selectedContactPeopleCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)initlizeValue:(CellModel *)model {
    self.nameLabel.text = model.title;
    self.phoneLabel.text = model.subTitle;
    self.addPeopleImg.image = [UIImage imageNamed:model.image];
    self.delegate = model.delegate;
    self.nameTextfield.text = model.userInfo;
    self.phoneTextfield.text = model.selectorString;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = [UIColor whiteColor];
        [self initializeSubviews];
    }
    return self;
}

- (void)initializeSubviews {
    
    /*
     *  选择联系人框框
     */
    UIView *addContactView = [[UIView alloc] initWithFrame:CGRectZero];
//    addContactView.backgroundColor=[UIColor  yellowColor];
    [self.contentView addSubview:addContactView];
    [addContactView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView.mas_right);
        make.top.equalTo(self.contentView.mas_top);
        make.bottom.equalTo(self.contentView.mas_bottom);
        make.width.equalTo(@(80));
    }];
    
    /*
     * 收货人
     */
    _nameLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    _nameLabel.font = [UIFont systemFontOfSize:15];
    _nameLabel.textColor = BK_MY_COLOR_07;
    [self.contentView addSubview:_nameLabel];
    
    /*
     * 联系方式
     */
    _phoneLabel = [[UILabel alloc] initWithFrame:CGRectZero];
//    _phoneLabel.backgroundColor=[UIColor  yellowColor];
    _phoneLabel.textColor = BK_MY_COLOR_07;
    _phoneLabel.font = [UIFont systemFontOfSize:15];
    [self.contentView addSubview:_phoneLabel];
    
    self.selectedContactsButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.selectedContactsButton.frame = CGRectMake(0.5, 0, 79.5, 90);
    [addContactView addSubview:self.selectedContactsButton];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectedContactsClick:)];
    [self.selectedContactsButton addGestureRecognizer:tap];
    
    _addPeopleImg = [[UIImageView alloc] initWithFrame:CGRectZero];
    [self.selectedContactsButton addSubview:_addPeopleImg];
    _addPeopleImg.userInteractionEnabled = YES;
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    [self.selectedContactsButton addSubview:titleLabel];
    titleLabel.text = @"选择联系人";
    titleLabel.font = [UIFont systemFontOfSize:10.0];
    titleLabel.textColor = RGBCOLOR(155, 154, 158, 1);
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.userInteractionEnabled = YES;
    
    [_addPeopleImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.selectedContactsButton.mas_centerX);
        make.centerY.equalTo(self.selectedContactsButton.mas_centerY);
        make.width.equalTo(@(24));
        make.height.equalTo(@(26));
    }];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.selectedContactsButton.mas_centerX);
        make.top.equalTo(_addPeopleImg.mas_bottom).mas_offset(5);
        make.width.equalTo(self.selectedContactsButton.mas_width);
        make.height.equalTo(@(15));
    }];
    
    [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left).mas_offset(Address_BackGauge);
        make.width.equalTo(@(66));
        make.centerY.equalTo(self.contentView.mas_centerY).mas_offset(- 45 / 2);
    }];
    [_phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_nameLabel.mas_left);
        make.width.equalTo(@(66));
        make.centerY.equalTo(self.contentView.mas_centerY).mas_offset(45 / 2);
    }];
    
    self.nameTextfield = [[UITextField alloc] initWithFrame:CGRectZero];
    self.nameTextfield.delegate = self;
    self.nameTextfield.font = [UIFont systemFontOfSize:15];
    [self.nameTextfield addTarget:self action:@selector(textValueChanged:) forControlEvents:UIControlEventEditingChanged];
    [self.contentView addSubview:self.nameTextfield];
    [self.nameTextfield mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_nameLabel.mas_right).mas_offset(16);
        make.centerY.equalTo(_nameLabel.mas_centerY);
        make.height.equalTo(@(self.contentView.frame.size.height / 2));
        make.right.equalTo(addContactView.mas_left);
    }];
    
    self.phoneTextfield = [[UITextField alloc] initWithFrame:CGRectZero];
    self.phoneTextfield.delegate = self;
    self.phoneTextfield.tag = 666;
    self.phoneTextfield.font = [UIFont systemFontOfSize:15];
    [self.phoneTextfield addTarget:self action:@selector(textValueChanged:) forControlEvents:UIControlEventEditingChanged];
    [self.contentView addSubview:self.phoneTextfield];
    self.phoneTextfield.keyboardType = UIKeyboardTypeNumberPad;
    [self.phoneTextfield mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_phoneLabel.mas_right).mas_offset(15);
        make.centerY.equalTo(_phoneLabel.mas_centerY);
        make.height.equalTo(@(self.contentView.frame.size.height / 2));
        make.right.equalTo(addContactView.mas_left);
    }];
    
    UIView * centerLineView = [[UIView alloc] initWithFrame:CGRectZero];
    centerLineView.backgroundColor = BK_MY_COLOR_03;
    [self.contentView addSubview:centerLineView];
    [centerLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left);
        make.centerY.equalTo(self.contentView.mas_centerY);
        make.right.equalTo(addContactView.mas_left);
        make.height.equalTo(@(0.5));
    }];
    
    UIView * rightLineView = [[UIView alloc] initWithFrame:CGRectZero];
    rightLineView.backgroundColor = BK_MY_COLOR_03;
    [addContactView addSubview:rightLineView];
    [rightLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(addContactView.mas_left);
        make.width.equalTo(@(1.5));
        make.top.equalTo(addContactView.mas_top);
        make.bottom.equalTo(addContactView.mas_bottom);
    }];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.contentView addSubview:lineView];
    lineView.backgroundColor = BK_MY_COLOR_06;
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left);
        make.right.equalTo(self.contentView.mas_right);
        make.bottom.equalTo(self.contentView.mas_bottom);
        make.height.equalTo(@(1.5));
    }];
}

- (void)selectedContactsClick:(UITapGestureRecognizer *)click {
    int __block tip = 0;
    ABAddressBookRef addBook = nil;
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 6.0) {
        addBook = ABAddressBookCreateWithOptions(NULL, NULL);
        dispatch_semaphore_t semaphonre = dispatch_semaphore_create(0);
        ABAddressBookRequestAccessWithCompletion(addBook, ^(bool granted, CFErrorRef error) {
            if (!granted) {
                tip = 1;
            }
            dispatch_semaphore_signal(semaphonre);
        });
        dispatch_semaphore_wait(semaphonre, DISPATCH_TIME_FOREVER);
    } else {
    
        addBook = ABAddressBookCreate();
    }
    if (tip) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"请您设置允许BestKeep访问您的通讯录\n前往设置>隐私>通讯录" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
    } else {
        __weak typeof(self)weakSelf = self;
        if ([self.delegate respondsToSelector:@selector(addContactPeople:chooseContactPeople:)]) {
            [self.delegate addContactPeople:self chooseContactPeople:^(ABRecordRef person) {
                BK_my_ContactPeople *people = [[BK_my_ContactPeople alloc] init];
                [people parsePerson:person];
                weakSelf.nameTextfield.text = people.name;
                weakSelf.phoneTextfield.text = people.telephone;
                weakSelf.address.consigneeName = people.name;
                weakSelf.address.shippingTelephone = people.telephone;
            }];
        }
    }
}

- (void)setAddress:(BK_my_ShippingAddress *)address {
    _address = address;
    if (_address.consigneeName) {
        self.nameTextfield.text = _address.consigneeName;
    }
    if (_address.shippingTelephone) {
        self.phoneTextfield.text = _address.shippingTelephone;
    }
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    
        if (textField == self.nameTextfield) {
        if ([textField.text isEqualToString:self.address.consigneeName]) {
            return;
        }
        self.address.consigneeName = textField.text;
    } else if (textField == self.phoneTextfield) {
        if ([textField.text isEqualToString:self.address.shippingTelephone]) {
            return;
        }
        self.address.shippingTelephone = textField.text;
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (textField.tag == 666) {
        if (textField.text.length - range.length + string.length > 11) {
            return NO;
        }
    }
    return YES;
}

- (void)textValueChanged:(UITextField *)textField{
    if ([self.delegate respondsToSelector:@selector(addContactPeople:inputEnd:)]) {
        [self.delegate addContactPeople:self inputEnd:self.address];
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (IOS8) {
        NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
        if ([[UIApplication sharedApplication] canOpenURL:url]) {
            [[UIApplication sharedApplication] openURL:url];
        }
    }
}

@end















































